package com.yonyougov.devops.cruxcisupport.button;

import com.yonyougov.devops.cruxcisupport.button.dto.*;
import com.yonyougov.devops.cruxcisupport.button.vo.ButtonByMenuIdRespVo;
import com.yonyougov.devops.cruxcisupport.button.vo.CurrentUserButtonByMenuCodeDto;
import com.yonyougov.devops.cruxcisupport.button.vo.NoLinkButtonByRoleIdDto;
import com.yonyougov.devops.cruxcisupport.button.vo.NoLinkButtonByRoleIdVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.List;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "按钮模块", tags = "按钮模块")
@RestController
@RequestMapping("/api/v2/button")
public class ButtonController extends CrudController<ButtonEntity, BigInteger> {

    private ButtonService buttonService;

    ButtonController(ButtonService buttonService) {
        this.buttonService = buttonService;
    }

    @Override
    public BaseService<ButtonEntity, BigInteger> getService() {
        return buttonService;
    }

    //<!--用户自定义代码开始-->

    @ApiModelProperty(value = "按钮管理模块新增按钮调用", notes = "增加一个按钮")
    @PostMapping("/b/add")
    @SneakyThrows
    public BaseResponseDto add(@RequestBody @Valid AddButtonRequestDto addButtonRequestDto) {
        ButtonEntity buttonEntity = new ButtonEntity();
        BeanUtils.copyProperties(addButtonRequestDto, buttonEntity);

        buttonService.add(buttonEntity);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiModelProperty(value = "按钮管理模块删除按钮调用", notes = "删除一个按钮")
    @PostMapping("/b/delete/id")
    @SneakyThrows
    public BaseResponseDto delete(@RequestBody @Valid DeleteButtonRequestDto deleteButtonRequestDto) {
        buttonService.deleteOneButtonByButtonId(deleteButtonRequestDto.getId());

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "进入按钮管理模块时首次渲染调用", notes = "分页查询全部")
    @PostMapping(value = "/b/single/page/like")
    public BaseResponseDto findAllSinglePage(@RequestBody @Valid AllButtonPageLikeRequestDto request) {
        ButtonEntity buttonEntity = new ButtonEntity();
        BeanUtils.copyProperties(request, buttonEntity);

        Pagination<ButtonEntity> buttonEntityPagination = buttonService.likeAllBy(buttonEntity);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", buttonEntityPagination.getTotalCount());
        result.put("data", buttonEntityPagination.getDatas());
        return result;
    }

    @ApiModelProperty(value = "角色关联按钮时候调用", notes = "根据角色id获取未被该角色关联的按钮")
    @PostMapping(value = "/b/all/nolink/roleid")
    public BaseResponseDto findNoLinkButtonByRoleId(@RequestBody @Valid NoLinkButtonRequestDto request) {
        NoLinkButtonByRoleIdDto noLinkButtonByRoleIdDto = new NoLinkButtonByRoleIdDto();
        BeanUtils.copyProperties(request, noLinkButtonByRoleIdDto);

        Pagination<NoLinkButtonByRoleIdVo> pagination = buttonService.findNoLinkButtonByRoleId(noLinkButtonByRoleIdDto);

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("totalCount", pagination.getTotalCount());
        baseResponseDto.put("data", pagination.getDatas());
        return baseResponseDto;
    }


    @ApiModelProperty(value = "当点击某个菜单时候，根据用户权限去渲染按钮时候调用", notes = "可选择根据菜单code获取当前用户已经关联的按钮")
    @SneakyThrows
    @PostMapping("/b/all/menuid")
    public BaseResponseDto findButtonByMenuCode(@RequestBody @Valid ButtonByMenuIdRequestDto request) {
        CurrentUserButtonByMenuCodeDto currentUserButtonByMenuCodeDto = new CurrentUserButtonByMenuCodeDto();
        BeanUtils.copyProperties(request, currentUserButtonByMenuCodeDto);

        List<ButtonByMenuIdRespVo> datas = buttonService.findCurrentUserButtonByMenuCode(currentUserButtonByMenuCodeDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", datas);
        return result;
    }

    //<!--用户自定义代码结束-->
}
